package ua.kpi_java_training.morochenets.module2;

/**
 * Class which sets install a pool and performs actions with him
 *
 * @author Ari100tell
 * @data: 01.06.13
 * @time: 17:06
 */
public class Pool {
    private final int LOWER_BOUND = 0;
    private final int PREY_INDEX = 1;
    private final int PREDATOR_INDEX = -1;
    private int positionWidth = 0;
    private int positionLength = 0;
    private int width;
    private int length;
    private int numberPreys;
    private int numberPredators;
    private PlaceForLife placeForLife=new PlaceForLife(width,length);
    protected Pool(int width, int length, int numberPreys, int numberPredators) {
        this.width = width;
        this.length = length;
        this.numberPreys = numberPreys;
        this.numberPredators = numberPredators;

        System.out.println("width=" + this.width);
        System.out.println("length=" + this.length);
    }

    public void completionPool() {
        placeForLife.init();
        addPreys();
        addPredators();
        //placeForLife.outputPool(placesForLife);
    }

    private void addPreys() {
        for (int i = 0; i < numberPreys; i++) {
            addFish();
            placeForLife.setPlace(PREY_INDEX,positionWidth,positionLength);
        }
    }

    private void addPredators() {
        for (int i = 0; i < numberPredators; i++) {
            addFish();
            placeForLife.setPlace(PREDATOR_INDEX,positionWidth,positionLength);
        }
    }

    private void addFish() {
        do {
            setPositions();
        } while (placeForLife.getPlace(positionWidth,positionLength) != 0);
    }



    private void setPositions() {
        Randomization randomization = new Randomization();
        positionLength = randomization.getRandom(LOWER_BOUND, length);
        positionWidth = randomization.getRandom(LOWER_BOUND, width);
    }

    public int getWidth() {
        return width;
    }

    public int getLength() {
        return length;
    }
}
